<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="isLoading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>{{ company.name }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex">
              <!-- 两个内容 -->
              <el-col :span="12">负责人</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <!-- 头部添加新部门 -->
                <!-- <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addGradeOneDept">添加新部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown> -->

                <!-- 测试 菜单功能-->
                <el-dropdown @command="handleCommand">
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item :command="{type: 'add',}">添加子部门</el-dropdown-item>
                    <el-dropdown-item :command="{type: 'edit'}">修改子部门</el-dropdown-item>
                    <!-- 通过作用域获取data.id -->
                    <el-dropdown-item :command="{type: 'del',}">删除子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <template #default="{data}">
            <!-- 用了一个行列布局 -->
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%;">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex">
                  <!-- 两个内容 -->
                  <el-col :span="12">{{ data.manager }}</el-col>
                  <el-col :span="12">
                    <!-- 下拉菜单 element -->
                    <el-dropdown @command="handleCommand">
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item :command="{type: 'add',data:data}">添加子部门</el-dropdown-item>
                        <el-dropdown-item :command="{type: 'edit',id: data.id}">修改子部门</el-dropdown-item>
                        <!-- 通过作用域获取data.id -->
                        <el-dropdown-item :command="{type: 'del',id: data.id}">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 添加弹窗组件 -->
      <addDept
        ref="addDept"
        :show-dialog.sync="showDialog"
        :node-data="nodeData"
        :departs-list="departsList"
        @addSuccess="getDepartmentList"
      />
    </div>
  </div>
</template>

<script>
import addDept from './components/add-dept.vue'

// import { getDepartmentsApi,delDepartmentApi } from '@/api/departments'
// import { transListToTreeData } from '@/utils'

export default {
  name: 'Departments',
  components: {
    addDept
  },
  data() {
    return {
      company: {
        name: ''
      },
      departs: [],
      // 存放组织家国中处理之前的所有数据
      departsList: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      // 表示当前点击到的这一行的数据
      nodeData: {},
      isLoading: false
    }
  },
  created() {
    this.getDepartmentsList()
  },
  methods: {
    /**
     * 添加新的一级部门
     */
    addGradeOneDept() {
      this.showDialog = true
      this.nodeData = this.company
      // 通过 ref 调用子组件中的获取负责人方法
      this.$refs.addDept.getDepartmentManagerList()
    },
    handleCommand({ type, id, data }) {
      // console.log(type)
      if (type === 'add') {
        this.openDialog(data)
      }
      if (type === 'edit') {
        this.editDepartment(id)
      }
      if (type === 'del') {
        this.delDepartment(id)
      }
    },
    /**
     * 编辑部门
     */
    editDepartment(id) {
      this.showDialog = true
      // 通过 ref 调用子组件中的获取部门详细方法，并修改弹窗标题
      this.$refs.addDept.getDepartmentDetail(id)
      this.$refs.addDept.title = '编辑部门'
    },
    /**
     * 添加部门
     */
    openDialog(data) {
      this.showDialog = true
      // 赋值给nodeData
      this.nodeData = data
      // 通过 ref 调用子组件中的获取负责人方法，并修改弹窗标题
      // this.$refs.addDept.getDepartmentManagerList()
      this.$refs.addDept.title = '添加部门'
    },
    closeDialog() {
      this.showDialog = false
    },
    /**
     * 删除部门
     * @param id
     */
    delDepartment(id) {
      this.$confirm('确定要删除此数据吗？', '温馨提示').then(async() => {
        // 点击了确定
        // await delDepartmentApi(id) //调用删除接口
        this.$message.success('删除成功')
        this.getDepartmentsList()
      }).catch(() => {
        // 点击了取消
      })
    },
    /**
     * 获取组织架构列表
     */
    async getDepartmentsList() {
      // 开启 loading 效果
      // this.isLoading = true
      // const res = await getDepartmentsApi()
      // 导入完整结构组织列表
      // 组织结构数组接口无法使用
      // this.departsList = JSON.parse(JSON.stringify(res.data.depts)) // 进行一个深拷贝，通过特性丢失undefined和函数去除children
      // this.departs = transListToTreeData(res.data.depts, '') // 通过导入的函数方法，不是当前页面的函数方法，不需要通过this来调用
      // console.log('树形列表', this.departs)
      // this.company.name = res.data.companyName
      // 收集所有的一级部门名称
      // this.company.children = this.departs
      // this.company.id = ''
      // console.log('组织数据', res)
      // this.isLoading = false
    }

  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}

// 优化树形组件，展开显示 减号图标，收缩显示 ➕图标
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
